package com.gc.web.controller.business;

import cn.hutool.core.date.DateUtil;
import com.gc.business.domain.ProjectItem;
import com.gc.business.domain.UpkeepProject;
import com.gc.business.domain.UpkeepTemplate;
import com.gc.business.service.IUpkeepProjectService;
import com.gc.business.service.IUpkeepTemplateService;
import com.gc.common.annotation.Log;
import com.gc.common.base.Result;
import com.gc.common.enums.BusinessType;
import com.gc.common.support.Convert;
import com.gc.common.utils.poi.ExcelUtil;
import com.gc.framework.util.ShiroUtils;
import com.gc.framework.web.base.BaseController;
import com.gc.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 电梯保养模板 控制层
 * 
 * @author hruiyan
 * @date 2020-11-18
 */
@Slf4j
@Controller
@RequestMapping("/business/upkeepTemplate")
public class UpkeepTemplateController extends BaseController {

    private String prefix = "business/upkeepTemplate";

	@Autowired
	private ISysUserService userService;
	
	private final IUpkeepTemplateService upkeepTemplateService;
	private final IUpkeepProjectService upkeepProjectService;

	@Autowired
	public UpkeepTemplateController(IUpkeepTemplateService upkeepTemplateService,
									IUpkeepProjectService upkeepProjectService) {
		this.upkeepTemplateService = upkeepTemplateService;
		this.upkeepProjectService = upkeepProjectService;
	}

	/**
	*  列表页
	*/
	@RequiresPermissions("business:upkeepTemplate:view")
	@GetMapping
	public String upkeepTemplate(){
	    return prefix + "/upkeepTemplate";
	}

	/**
	 * 新增页
	 */
	@GetMapping("/add")
	public String add(){
		return prefix + "/add";
	}

    /**
     * 拷贝新增页
     * @return
     */
	@GetMapping("/addCopy")
	public String addCopy(Long templateId,ModelMap mmap){
        UpkeepTemplate upkeepTemplate = upkeepTemplateService.getById(templateId);
        //查询项目清单列表
        mmap.put("projectItem_list_0",upkeepProjectService.list(new UpkeepProject(){{setTemplateId(upkeepTemplate.getTemplateId());setProjectType("0");}}));
        mmap.put("projectItem_list_1",upkeepProjectService.list(new UpkeepProject(){{setTemplateId(upkeepTemplate.getTemplateId());setProjectType("1");}}));
        mmap.put("projectItem_list_2",upkeepProjectService.list(new UpkeepProject(){{setTemplateId(upkeepTemplate.getTemplateId());setProjectType("2");}}));
        mmap.put("projectItem_list_3",upkeepProjectService.list(new UpkeepProject(){{setTemplateId(upkeepTemplate.getTemplateId());setProjectType("3");}}));
        mmap.put("projectItem_list_4",upkeepProjectService.list(new UpkeepProject(){{setTemplateId(upkeepTemplate.getTemplateId());setProjectType("4");}}));

        upkeepTemplate.setTemplateId(null);
        mmap.put("upkeepTemplate", upkeepTemplate);
	    return prefix + "/edit";
    }

	/**
	 * 编辑页
	 */
	@GetMapping("/edit/{templateId}")
	public String edit(@PathVariable("templateId") Long templateId, ModelMap mmap){
		UpkeepTemplate upkeepTemplate = upkeepTemplateService.getById(templateId);
		mmap.put("upkeepTemplate", upkeepTemplate);

		//查询项目清单列表
		mmap.put("projectItem_list_0",upkeepProjectService.list(new UpkeepProject(){{setTemplateId(upkeepTemplate.getTemplateId());setProjectType("0");}}));
		mmap.put("projectItem_list_1",upkeepProjectService.list(new UpkeepProject(){{setTemplateId(upkeepTemplate.getTemplateId());setProjectType("1");}}));
		mmap.put("projectItem_list_2",upkeepProjectService.list(new UpkeepProject(){{setTemplateId(upkeepTemplate.getTemplateId());setProjectType("2");}}));
		mmap.put("projectItem_list_3",upkeepProjectService.list(new UpkeepProject(){{setTemplateId(upkeepTemplate.getTemplateId());setProjectType("3");}}));
		mmap.put("projectItem_list_4",upkeepProjectService.list(new UpkeepProject(){{setTemplateId(upkeepTemplate.getTemplateId());setProjectType("4");}}));

		return prefix + "/edit";
	}
	
	/**
	 * 查询电梯保养模板列表
	 */
	@RequiresPermissions("business:upkeepTemplate:list")
	@PostMapping("/list")
	@ResponseBody
	public Result list(UpkeepTemplate upkeepTemplate){

		Long attrId = getAttrId();
		Boolean ifCompany = userService.ifCompanyRole(attrId);

		if(ifCompany){
			//关联公司查询
			upkeepTemplate.setCompanyId(attrId);
			startPage();
			List<UpkeepTemplate> list = upkeepTemplateService.list(upkeepTemplate);
			return Result.success(getDataTable(list));
		}else{
			return Result.success(getDataTable(new ArrayList<UpkeepTemplate>()));
		}
	}

	/**
	 * 新增保存电梯保养模板
	 */
	@RequiresPermissions("business:upkeepTemplate:add")
	@Log(title = "电梯保养模板", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	@Transactional(rollbackFor = Exception.class)
	public Result addSave(UpkeepTemplate upkeepTemplate,ProjectItem projectItem){
		Long attrId = getAttrId();
		upkeepTemplate.setCompanyId(attrId);
		upkeepTemplate.setCreateBy(ShiroUtils.getLoginName());
		upkeepTemplate.setCreateTime(new Date());
		upkeepTemplateService.save(upkeepTemplate);
		//保存项目清单
		this.upkeepProjectService.saveProjectItemList(projectItem,upkeepTemplate.getTemplateId());
		return Result.success(upkeepTemplate);
	}

	/**
	 * 修改保存电梯保养模板
	 */
	@RequiresPermissions("business:upkeepTemplate:edit")
	@Log(title = "电梯保养模板", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	@Transactional(rollbackFor = Exception.class)
	public Result editSave(UpkeepTemplate upkeepTemplate,ProjectItem projectItem){
		upkeepTemplate.setUpdateBy(ShiroUtils.getSysUser().getUserName());
		upkeepTemplate.setUpdateTime(DateUtil.date());
		upkeepTemplateService.updateById(upkeepTemplate);
		//初始化项目清单
		this.upkeepProjectService.deleteByTemplateId(upkeepTemplate.getTemplateId());
		//保存项目清单
		this.upkeepProjectService.saveProjectItemList(projectItem,upkeepTemplate.getTemplateId());
		return Result.success(upkeepTemplate);
	}
	
	/**
	 * 删除电梯保养模板
	 */
	@RequiresPermissions("business:upkeepTemplate:remove")
	@Log(title = "电梯保养模板", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
    @Transactional(rollbackFor = Exception.class)
	public Result remove(String ids){
		Long[] longIds = Convert.toLongArray(ids);
        for (Long longId : longIds) {
            //初始化项目清单
            this.upkeepProjectService.deleteByTemplateId(longId);
        }
		upkeepTemplateService.removeByIds(Arrays.asList(longIds));
		return Result.success();
	}

	/**
	 * 导出电梯保养模板列表
	 */
	@RequiresPermissions("business:upkeepTemplate:export")
	@PostMapping("/export")
	@ResponseBody
	public Result export(UpkeepTemplate upkeepTemplate){
		List<UpkeepTemplate> list = upkeepTemplateService.list(upkeepTemplate);
		ExcelUtil<UpkeepTemplate> util = new ExcelUtil<>(UpkeepTemplate.class);
		util.exportExcel(list, "upkeepTemplate");
		return Result.success();
	}
}
